package com.intel.factorytest;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DatabaseHelper";
    private static final String DATABASE_NAME = "testresult.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_SINGLE_TEST = "single";
    public static final String TABLE_SYSTEM_TEST = "system";
    public static final String TABLE_PCB_TEST = "pcb";

    public static final String PROJECTION_ID = "id";
    public static final String PROJECTION_NAME = "name";
    public static final String PROJECTION_RESULT = "result";
    final Context mContext;

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
    }

    public void onCreate(SQLiteDatabase db) {
        Log.e(TAG, "DatabaseHelper     onCreate");
        try {
            db.execSQL("CREATE TABLE " + TABLE_SINGLE_TEST + " ("
                    + PROJECTION_ID + " INTEGER," + PROJECTION_NAME + " TEXT,"
                    + PROJECTION_RESULT + " INTEGER" + ");");

            db.execSQL("CREATE TABLE " + TABLE_SYSTEM_TEST + " ("
                    + PROJECTION_ID + " INTEGER," + PROJECTION_NAME + " TEXT,"
                    + PROJECTION_RESULT + " INTEGER" + ");");

            db.execSQL("CREATE TABLE " + TABLE_PCB_TEST + " (" + PROJECTION_ID
                    + " INTEGER," + PROJECTION_NAME + " TEXT,"
                    + PROJECTION_RESULT + " INTEGER" + ");");
        } catch (SQLException e) {
            Log.e(TAG, e.toString());
        }

    }

    // private void InsertResultToDatabase(SQLiteDatabase db, int mType,
    // int position, String mItemName, int mTestResult) {
    // try {
    // StringBuilder builder = new StringBuilder();
    // String mResultTable;
    // if (mType == ResultDataSave.SINGLE_ITEM_TEST) {
    // mResultTable = TABLE_SINGLE_TEST;
    // } else if (mType == ResultDataSave.SYSTEM_ITEM_TEST) {
    // mResultTable = TABLE_SYSTEM_TEST;
    // } else if (mType == ResultDataSave.PCB_ITEM_TEST) {
    // mResultTable = TABLE_PCB_TEST;
    // } else {
    // mResultTable = TABLE_SINGLE_TEST;
    // }
    // builder.append("insert into ")
    // .append(mResultTable)
    // .append(" (" + PROJECTION_ID + ", " + PROJECTION_NAME
    // + ", " + PROJECTION_RESULT + ") values('")
    // .append(Integer.toString(position)).append("', '")
    // .append(mItemName).append("', '")
    // .append(Integer.toString(mTestResult)).append("');");
    // db.execSQL(builder.toString());
    // } catch (SQLException e) {
    // Log.e(TAG, e.toString());
    // }
    // }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_SINGLE_TEST);
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_SYSTEM_TEST);
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_PCB_TEST);
        onCreate(db);
    }
}
